//
//  ApplyBuildActivityViewController.m
//  shise
//
//  Created by 徐国庆 on 14/12/27.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "ApplyBuildActivityViewController.h"
#import "NetHandler.h"
#import "Food.h"


@interface ApplyBuildActivityViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;

@property(nonatomic, retain)NSMutableArray *array;

@end

@implementation ApplyBuildActivityViewController

- (void)dealloc
{
    [_tableView release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    [self handleData];
    
    self.title = @"申请创建活动";
}

// 设置每个区中cell的个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"%ld", self.array.count);
//    if (section == 0) {
//        
//        return 1;
//    } else {
//        
//        return self.array.count ;
//    }
    return self.array.count;
}

// 为每一个cell添加数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    NSString *string = @"applyBuildActivity";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string]autorelease];
    }
    if ([food.type isEqualToString:@"ContentCard"]) {
        
        cell.textLabel.text = food.content;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.alpha = 0.8;
        cell.textLabel.numberOfLines = 2;
        return cell;
    } else if ([food.type isEqualToString:@"PicCard"]){
        
        cell.textLabel.text = food.userName;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
        cell.textLabel.alpha = 0.8;
        cell.detailTextLabel.text = food.desc;
        cell.detailTextLabel.alpha = 0.5;
//        NSLog(@"主标题%@", cell.textLabel.text);
//        NSLog(@"副标题%@", cell.detailTextLabel.text);
        return cell;
        
    } else {
        return cell;
    }
    
}

// 设置cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Food *food = [self.array objectAtIndex:indexPath.row];
    if ([food.type isEqualToString:@"PicCard" ] || [food.type isEqualToString:@"ContentCard"]) {
        
        return 55;
    } else {
        
        return 0;
    }
//    return 44;
}
//
//// 设置区的个数
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    return 2;
//}
//// 设置区名
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return @"";
//}
//
//// 设置区的高度
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 5;
//}

//- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
//{
//    return 10;
//}

// 自定义处理数据的方法
- (void)handleData
{
    NSString *address = @"https://api.shiseapp.com/v2/topic/lst/?_city=%E5%A4%A7%E8%BF%9E&_device=Matcha%20v2.5.0%20%28iPhone%204S,%207.1%29&_version=2.5.0&bid=8&conf_ver=1419610425&gps=38.883430,121.540160&uuid=44cd1914a02fab9e63fb7ec1fbf910e0";
    [NetHandler getDataWithUrl:address completion:^(NSData *data) {
        
        NSError *error = nil;
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSArray *arr = [dic objectForKey:@"data"];
        for (NSDictionary *tempDic in arr) {
            
            Food *food = [[Food alloc] initWithDictionary:tempDic];
//            NSLog(@"+++++%@", food.userName);
//            NSLog(@"----------%@", food.desc);
            [self.array addObject:food];
        }
        
        [self.tableView reloadData];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
